package com.daviteq.fw.controller.validator;

import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

import com.daviteq.fw.controller.form.LoginForm;

public class LoginFormValidator implements Validator {
    LoginForm loginForm;

    @Override
    public boolean supports(Class<?> clazz) {
        return LoginFormValidator.class.isAssignableFrom(clazz);
    }

    @Override
    public void validate(Object commandObject, Errors errors) {
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "userName", "Field is required.");
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "password", "Field is required.");

        loginForm = (LoginForm) commandObject;

        if ((loginForm.getPassword() != "password") && (loginForm.getUserName() != "partha")) {
            errors.reject("Credentials provided are not correct.");
        }
    }
}
